﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace OpenSees.XML
{
    /// <summary>
    /// 单元分析结果序列化容器
    /// </summary>
    [XmlRoot("OpenSees")]
    public class ElementXML : BasicXML
    {
        /// <summary>
        /// Recorder记录的分析结果
        /// </summary>
        private enum ResultType
        {
            /// <summary>
            /// 单元分析结果
            /// </summary>
            Element = 0,

            /// <summary>
            /// 截面分析结果
            /// </summary>
            Section = 1,

            /// <summary>
            /// 材料分析结果
            /// </summary>
            Material = 2,

            /// <summary>
            /// 信息为空
            /// </summary>
            Empty = -1,
        }

        /// <summary>
        /// 单元响应
        /// </summary>
        [XmlElement("ElementOutput")]
        public List<ElementResponse> ElementOutputs { set; get; }

        [XmlIgnore]
        /// <summary>
        /// 获得分析结果
        /// </summary>
        /// <returns></returns>
        private ResultType GetResultType
        {
            get
            {
                //是否存在单元
                if (this.ElementOutputs == null || this.ElementOutputs.Count == 0) return ResultType.Empty;
                //遍历单元对象
                foreach (var element in this.ElementOutputs)
                {
                    //是否存在单元层次分析结果
                    if (!element.isEmpty) return ResultType.Element;
                    //是否存在截面、材料层次分析结果
                    else if (element.GaussPointOutput != null && element.GaussPointOutput.SectionOutput != null)
                    {
                        //截面层次分析结果
                        if (!element.GaussPointOutput.SectionOutput.isEmpty) return ResultType.Section;
                        //材料层次分析结果
                        else if (element.GaussPointOutput.SectionOutput.FiberOutput != null &&
                            element.GaussPointOutput.SectionOutput.FiberOutput.MaterialOut != null &&
                            !element.GaussPointOutput.SectionOutput.FiberOutput.MaterialOut.isEmpty)
                            return ResultType.Material;
                    }
                }
                return ResultType.Empty;
            }
        }

        /// <summary>
        /// 获得响应基类列表
        /// </summary>
        /// <returns></returns>
        public override List<Response> GetResponseList(bool includeTime)
        {
            //初始化
            var responseList = this.GetBasicResponseList(includeTime);
            //判断类型
            var type = this.GetResultType;
            //为空直接返回
            if (type == ResultType.Empty) return responseList;
            //遍历单元
             this.ElementOutputs.ForEach(element =>
            {
                //单元响应
                if (type == ResultType.Element && !element.isEmpty) responseList.Add(element);
                //高斯积分点存在
                if (element.GaussPointOutput != null && element.GaussPointOutput.SectionOutput != null)
                {
                    //获得高斯积分点
                    var section = element.GaussPointOutput.SectionOutput;
                    //截面响应
                    if (type == ResultType.Section && !section.isEmpty)
                    {
                        section.eleTag = element.eleTag;
                        responseList.Add(section);
                    }
                    //材料响应
                    else if (type == ResultType.Material && section.FiberOutput != null &&
                    section.FiberOutput.MaterialOut != null && !section.FiberOutput.MaterialOut.isEmpty)
                    {
                        section.FiberOutput.MaterialOut.eleTag = element.eleTag;
                        responseList.Add(section.FiberOutput.MaterialOut);
                    }
                }
            });
            //返回值
            return responseList;
        }

        [XmlIgnore]
        /// <summary>
        /// 是否完整的分析结果
        /// </summary>
        /// <returns></returns>
        public override bool isObjectDeserialize
        {
            get
            {
                //是否存在单元
                if (this.ElementOutputs == null || this.ElementOutputs.Count == 0) return false;
                //获得分析结果类型
                return this.GetResultType != ResultType.Empty;
            }
        }
    }
}
